// import {} from './constants';
import {LOGIN_ACTION} from './constants';

import NetUtil from '../../common/NetUtil';
import config from '../../config/config';
import {setConfigValue, configkey,showLoading,hideLoading} from '../../utils/NativeUtil';

import {setUserToken} from '../main/actions';

export const loginAction = (loginId, password, success, error) => {
    return dispatch => {
        const params = {
            username: loginId,
            password: password
        };

        let url = "/auth/shoplogin";
        showLoading("正在登录");
        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            hideLoading();
            if (res.success) {
                //掉原生接口  将数据保存到数据库
                let data = res.data;
                let user = data.user;
                let token = data.token;
                dispatch(setUserToken(token));
                setConfigValue(configkey.UserToken, token);
                setConfigValue(configkey.LoginState, "1");
                // setConfigValue(configkey.Mobile, user.mobile);
                setConfigValue(configkey.UserHead, user.portrait);
                // setConfigValue(configkey.UserSex, user.sex);
                setConfigValue(configkey.UserName, user.username);
                // 一人一店模式
                if (data.shops.length > 0) {
                    let shop = data.shops[0];
                    setConfigValue(configkey.ShopAddress, shop.address);
                    setConfigValue(configkey.ShopLogo, shop.logo);
                    setConfigValue(configkey.ShopName, shop.shopName);
                    setConfigValue(configkey.ShopToken, shop.shopToken);
                }
                success();
            } else {
                error(res.message);
            }
        })
    }
};



